#![allow(dead_code)]
pub struct Solution;

impl Solution {
    pub fn longest_palindrome(s: String) -> String {
        let len = s.len();
        let mut start = 0;
        let mut end = 0;

        for i in 0..len {
            let len1 = expand_around_center(&s, i, i);
            let len2 = expand_around_center(&s, i, i + 1);
            let max_len = len1.max(len2);

            if max_len > end - start {
                start = i - (max_len - 1) / 2;
                end = i + max_len / 2;
            }
        }

        fn expand_around_center(s: &String, left: usize, right: usize) -> usize {
            let mut l = left;
            let mut r = right;

            while r < s.len() && s.chars().nth(l) == s.chars().nth(r) {
                l -= 1;
                r += 1;
            }

            r - l - 1
        }
        s[start..=end].to_string()
    }
}

#[cfg(test)]
mod tests {
    use super::Solution;

    #[test]
    fn test_longest_palindrome() {
        // Test case 1: Palindrome string
        assert_eq!(
            Solution::longest_palindrome(String::from("level")),
            String::from("level")
        );

        // Test case 2: Non-palindrome string
        assert_eq!(
            Solution::longest_palindrome(String::from("hello")),
            String::from("l")
        );

        // Test case 3: Empty string
        assert_eq!(
            Solution::longest_palindrome(String::from("")),
            String::from("")
        );

        // Test case 4: String with one character
        assert_eq!(
            Solution::longest_palindrome(String::from("a")),
            String::from("a")
        );

        // Test case 5: String with all same characters
        assert_eq!(
            Solution::longest_palindrome(String::from("aaaaa")),
            String::from("aaaaa")
        );
    }
}
